package web.listChoice;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

public class ListChoiceHome extends WebPage{
	private static final long serialVersionUID = 1L;
	private List<Lista> listas;
	private Lista source;
	private String seleccionado;
	private MarkupContainer form;

	public ListChoiceHome() {
		inicializar();
		this.add(form = new Form<Void>("form").add(
			lista(),
			listChoice(),
			seleccionado()
		));
		form.setOutputMarkupId(true);
	}

	private void inicializar() {
		this.listas = Arrays.asList(
				new Lista("saludos", "hola", "ciao", "hello", "alo", "aloha", "wazzzzzzuuuuuuuup"),
				new Lista("juegos", "Last Remnant", "Final Fantasy", "Valkyrie Profile"),
				new Lista("wasd", "wasd", "asd", "zxc", "123")
		);
		this.source = this.listas.get(0);
	}

	private Component lista() {
		IModel<List<Lista>> source = new PropertyModel<List<Lista>>(this, "listas");
		IModel<Lista> modelo = new PropertyModel<Lista>(this, "source");
		IChoiceRenderer<Lista> renderer = new ChoiceRenderer<Lista>("nombre");
		Component lista = new ListChoice<Lista>("lista", modelo, source, renderer);
		lista.add(ajaxFirstOnChange());
		return lista;
	}

	private Component listChoice() {
		IModel<String> modelo = new PropertyModel<String>(this, "seleccionado");
		IModel<List<String>> source = new PropertyModel<List<String>>(this, "source.lista");
		ListChoice<String> listChoice = new ListChoice<String>("listChoice", modelo, source);
		listChoice.add(ajaxSecondOnChange());
		return listChoice;
	}

	protected AjaxFormComponentUpdatingBehavior ajaxFirstOnChange() {
		return new AjaxFormComponentUpdatingBehavior("onchange") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				seleccionado = null;
				target.add(form);
			}
		};
	}
	
	protected AjaxFormComponentUpdatingBehavior ajaxSecondOnChange() {
		return new AjaxFormComponentUpdatingBehavior("onchange") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.add(form);
			}
		};
	}

	private Component seleccionado() {
		IModel<String> modelo = new PropertyModel<String>(this, "seleccionado");
		return new Label("seleccionado", modelo);
	}

	//getters&setters
	public List<Lista> getListas() {
		return listas;
	}
	public Lista getSource() {
		return source;
	}
	public String getSeleccionado() {
		return seleccionado;
	}
	public void setListas(List<Lista> listas) {
		this.listas = listas;
	}
	public void setSource(Lista source) {
		this.source = source;
	}
	public void setSeleccionado(String seleccionado) {
		this.seleccionado = seleccionado;
	}
}